package cn.micai.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @FileName: StringUtils
 * @Author: zhaoxinguo
 * @Date: 2018/12/3 12:04
 * @Description: 数组去重工具类
 */
public class StringUtils {

    // 数组去重方法1
    public static List<String> distinctA(String [] strings) {
        List<String> list = new ArrayList<String>(strings.length);
        for (String string : strings) {
            if (list.contains(string)) {
                continue;
            }
            list.add(string);
        }
        return list;
    }

    // 利用Set集合无序不可重复的特性进行元素过滤
    public static Object [] distinctB(String [] strings) {
        Set<String> set = new HashSet<String>();
        if (strings.length <= 0) {
            return null;
        }
        for (String string : strings) {
            set.add(string);
        }
        return set.toArray();
    }


    public static void main(String [] args) {
        String strings [] = {"a","b","c","a","e","f","b"};
        /*List<String> list = distinctA(strings);
        System.out.println(list.toString());*/
        Object[] objects = distinctB(strings);
        System.out.println(objects.toString());
    }
}
